package com.kevin.Code.BinarySearch;

/**
 * @author Vinlee Xiao
 * @Classname FindMinimuminRotatedSortedArrayii
 * @Description Leetcode 154. 寻找旋转排序数组中的最小值 II 中等难度 二分查找 一刷
 * @Date 2022/2/26 15:34
 * @Version 1.0
 */
public class FindMinimuminRotatedSortedArrayii {

    /**
     * 相比153 题增加了一种特殊的情况 学习分析题目的思路
     * 难点在于碰到 num[mid]==nums[privot]时无法碰到是处于最小值左边还是处于最小值右边
     * 解决办法:减少最右边重复数对于判断的影响
     *
     * @param nums
     * @return
     */
    public int findMin(int[] nums) {
        if (nums.length == 1) {
            return nums[0];
        }

        int low = 0;
        int high = nums.length - 1;
        //用于比较最右边的值
        int pivot = nums.length - 1;
        while (low <= high) {
            int mid = (low + high) >> 1;

            if (nums[mid] < nums[high]) {
                high = mid - 1;
            } else if (nums[mid] > nums[high]) {
                low = mid + 1;
            } else {
                pivot = pivot - 1;
                high = pivot;
            }
        }

        return nums[low];
    }

    public static void main(String[] args) {
        int[] nums = new int[]{1, 1};
        FindMinimuminRotatedSortedArrayii findMinimuminRotatedSortedArrayii = new FindMinimuminRotatedSortedArrayii();
        int min = findMinimuminRotatedSortedArrayii.findMin(nums);
        System.out.println(min);

    }
}
